﻿using System;
using System.Windows.Forms;
using System.Collections;
using Library;
using Gencode;
using Engine;

namespace GameTool
{
    public partial class DuaXe_From2 : Form
    {
        #region Properties
        private string _lop = string.Empty;
        public string Lop
        {
            set { _lop = value; }
            get { return _lop; }
        }

        private string _vong = string.Empty;
        public string Vong
        {
            set { _vong = value; }
            get { return _vong; }
        }

        private static DuaXe_From2 _instance = null;
        public static DuaXe_From2 Instance
        {
            set { _instance = value; }
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    _instance = new DuaXe_From2();
                }
                return _instance;
            }
        }
        #endregion

        #region Constructors
        public DuaXe_From2()
        {
            InitializeComponent();
            InitClass();
        }
        #endregion

        #region Methods
        public void ShowForm()
        {
            Vong = Lop = string.Empty;
            this.ShowDialog();
        }

        private void InitClass()
        {
            try
            {
                foreach (ClassInfo c in Common.ClassList)
                {

                    if (User.CheckPermission(c.Name)) // Kiem tra quyen duoc xem cua user
                    {
                        TabPage tab = new TabPage();
                        tab.Name = "lop" + c.Name;
                        tab.Text = "Lớp " + c.Name;
                        this.tabClass.TabPages.Add(tab);
                    }
                }
                if (Common.ClassList.Count > 0)
                {
                    tabClass_SelectedIndexChanged(null, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        // Ham dung de lay thong tin cua de thi
        private void GetExam(string className)
        {
            try
            {
                // Dua ra danh sach cac vong hien co
                ArrayList dirList = ftp_main.list_dir(Common.config.pathXml + @"\" + className + @"\Game5");
                if (dirList == null)
                {
                    return;
                }

                // Lay cac thu muc hien co tren server.
                foreach (string dir in dirList)
                {
                    if (dir.Contains("level"))
                    {
                        string prefix = dir.Substring(5);

                        TreeNode node = tvDeBai.Nodes.Add("Vòng " + prefix);
                        node.Name = dir;

                        ArrayList fileList = ftp_main.list_file(Common.config.pathXml + @"\" + className + @"\Game5\" + dir);

                        if (fileList == null)
                            return;

                        foreach (string file in fileList)
                        {
                            int index = file.LastIndexOf('.');
                            prefix = file.Substring(0, index);
                            TreeNode children = node.Nodes.Add("Đề " + prefix);
                            children.Name = prefix + "";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }
        #endregion

        #region Events
        private void tabClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                tvDeBai.Nodes.Clear();
                string classname = tabClass.TabPages[tabClass.SelectedIndex].Name;
                GetExam(classname);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }

        private void tvDeBai_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                TreeNode selectedNode = tvDeBai.SelectedNode;

                if (!selectedNode.Name.Contains("level"))
                {
                    Vong = selectedNode.Parent.Name;
                    Lop = tabClass.SelectedTab.Name;
                    Hide();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi!", "Canh bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Common.LogException(ex);
                Application.Exit();
            }
        }
        #endregion
    }
}
